package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理相关接口")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    @CacheEvict(value = "category_dish",key = "#dishDTO.categoryId")
    public Result insertDishWithFlavors(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品,菜品数据{}", dishDTO);
        dishService.insertDishWithFlavors(dishDTO);

        cleanCache(dishDTO.getCategoryId()+"");
        return Result.success();

    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(value = "category_dish",key = "#dishDTO.categoryId")
    public Result updateDishWithFlavors(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品,修改菜品{}", dishDTO);
        dishService.updateDishWithFlavors(dishDTO);
        cleanCache(dishDTO.getCategoryId()+"");
        return Result.success();

    }

    /**
     * 菜品的 分页条件查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品的 分页条件查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品的 分页条件查询,条件分页数据:{}", dishPageQueryDTO);

        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(value = "category_dish",allEntries = true)
    public Result deleteByIds(@RequestParam("ids") List<Long> ids) {
        log.info("批量删除菜品,要删除的id有{}", ids);
        dishService.deleteByIds(ids);

       // cleanCache("*");
        return Result.success();

    }


    /**
     * 根据id查询菜品所有信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品所有信息")
    public Result<DishVO> selectById(@PathVariable Long id) {
        log.info("根据id查询菜品所有信息,id为{}", id);
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);

    }

    /**
     * 起售禁售菜品
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售禁售菜品")
    @CacheEvict(value = "category_dish",allEntries = true)
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id) {
        dishService.startOrStop(status, id);
       // cleanCache("*");
        return Result.success();
    }

    /**
     * 根据分类id查询分类下所有启用的菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询分类下所有启用的菜品")
    public Result<List<Dish>> selectByCategoryId(Long categoryId) {
        log.info("根据分类id查询分类下所有启用的菜品,分类id{}", categoryId);
        List<Dish> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }


    /**
     * 清理category_dish 缓存
     */

    private  void cleanCache(String keyPattern){
        Set keys = redisTemplate.keys("category_dish_"+keyPattern);
        redisTemplate.delete(keys);
    }
}
